﻿using ConfExpert.Enums;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor.YeniYapi
{
    public partial class NumuneEtiketleri : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet ds;
        int EmptyLabelCount;
        public bool Stop = false;
        public NumuneEtiketleri()
        {
            InitializeComponent();

            foreach (XRTableCell tc in new XRTableCell[] { tcMarka1, tcMarka2, tcOrderRef1, tcOrderRef2,  tcKalite1, tcKalite2, tcTip1, tcTip2,  tcRecete3, tcRecete4, tcTarih1, tcTarih2, })
                tc.BeforePrint += ReSize; 
        }

        string MyRange;
        public NumuneEtiketleri(EtiketTipi tip, string range, int emptyLabelCount,int KopyaSayisi )
            : this()
        {
            EmptyLabelCount = emptyLabelCount;
            MyRange = range;
            if (tip == EtiketTipi.RollId)
                range = MakeCriteria(range);
            ds = Session.DataManager.ExecuteDataSet("GetNumuneEtiketi", tip, range, KopyaSayisi.ToString());

            if (tip == 0)
            {
                this.PrintingSystem.EndPrint += new EventHandler(PrintingSystem_EndPrint);
            }
            BindDataTabletoCells();
        }

        void BindDataTabletoCells()
        {
            for (int i = 0; i < EmptyLabelCount; i++)
            {
                DataRow r= ds.Tables["Table"].NewRow();
                ds.Tables["Table"].Rows.InsertAt(r, 0);
            }            

            this.DataMember = "Table";
            this.DataSource = ds;

            tcMarka1.DataBindings.Add("Text", null, "Table.POMarka");
            tcMarka2.DataBindings.Add("Text", null, "Table.POMarka");

            tcSipNo1.DataBindings.Add("Text", null, "Table.PONumber");
            tcSipNo2.DataBindings.Add("Text", null, "Table.PONumber");

            tcMArt1.DataBindings.Add("Text", null, "Table.POMArticle");
            tcMArt2.DataBindings.Add("Text", null, "Table.POMArticle");

            tcUArt1.DataBindings.Add("Text", null, "Table.POUArticle");
            tcUArt2.DataBindings.Add("Text", null, "Table.POUArticle");

            tcTarih1.DataBindings.Add("Text", null, "Table.Tarih");
            tcTarih2.DataBindings.Add("Text", null, "Table.Tarih");

            tcEn1.DataBindings.Add("Text", null, "Table.POEn", "{0:n0}");
            tcEn2.DataBindings.Add("Text", null, "Table.POEn", "{0:n0}");

            tcMt11.DataBindings.Add("Text", null, "Table.POMetraj", "{0:n0}");
            tcMt21.DataBindings.Add("Text", null, "Table.POMetraj", "{0:n0}");

            tcPid11.DataBindings.Add("Text", null, "Table.ProductId");
            tcPid21.DataBindings.Add("Text", null, "Table.ProductId");

            tcOrderRef1.DataBindings.Add("Text", null, "Table.PORef");
            tcOrderRef2.DataBindings.Add("Text", null, "Table.PORef");

            tcKalite1.DataBindings.Add("Text", null, "Table.POKalite");
            tcKalite2.DataBindings.Add("Text", null, "Table.POKalite");

            tcTip1.DataBindings.Add("Text", null, "Table.POTip");
            tcTip2.DataBindings.Add("Text", null, "Table.POTip");

            tcRecete3.DataBindings.Add("Text", null, "Table.POYikamaRecetesi");
            tcRecete4.DataBindings.Add("Text", null, "Table.POYikamaRecetesi");
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            Session.DataManager.ExecuteNonQuery("UpdGPNumuneSet", MyRange);
        }
        
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            EmptyLabelCount--;
        }

        private static string MakeCriteria(string range)
        {
            string[] parts = range.Replace(" ", String.Empty).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string str = String.Empty;
            foreach (string part in parts)
            {
                try
                {
                    int loc = part.IndexOf("-");
                    if (loc > -1)
                    {
                        int p1 = int.Parse("0" + part.Substring(0, loc).Replace("-", ""));
                        int p2 = int.Parse("0" + part.Substring(loc, part.Length - loc).Replace("-", ""));
                        if (p1 == 0 && p2 > 0)
                            str += " OR r.RollId <= " + p2.ToString();
                        else if (p1 > 0 && p2 == 0)
                            str += " OR r.RollId >= " + p1.ToString();
                        else
                            str += " OR r.RollId between " + p1.ToString() + " and " + p2.ToString();
                    }
                    else
                        str += " OR r.RollId = " + int.Parse(part).ToString();
                }
                catch
                {
                }
            }
            return str.Substring(4);
        }
        private void ReSize(object sender,PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 8.25f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.HeightF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.HeightF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        /*
        private void tcKalite2_BeforePrint(object sender, PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcKalite1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcMarka1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcMarka2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcOrderRef1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcOrderRef2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }
        */

        private void xrTable2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
                //foreach (XRTableRow row in xrTable2.Rows)
                //{
                //    foreach (XRTableCell cell in row.Cells)
                //    {
                //        cell.Text = "";
                //    }
                //}
            }
            
        }

        private void xrTable6_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

    }
}
